<?php

	// +----------------------------------------------------------------------
	// | 订单处理
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\event\ucenter;

	use app\model\store\Complaint as ComplaintModel;
	use app\common\event\Base;
	use app\model\store\Order as OrderModel;
	use app\event\store\Order as OrderEvent;
	use app\event\ucenter\Notice as NoticeEvent;
	use app\model\ucenter\User as UserModel;
	use think\facade\Db;
	use think\Exception;
	use think\facade\Log;

	class Complaint extends Base
	{

		/**
		 * 投诉订单
		 * @param array $param
		 * @return boolean
		 */
		public function add($param)
		{
			try {
				$order = OrderModel::where('order_sn', $param['order_sn'])->find();
				if (!$order) {
					throw new Exception('订单不存在');
				}
				if ($order['lock_status'] != 0) {
					throw new Exception('订单正在投诉处理中');
				}
				if (0 != $order['settlement_status']) {
					throw new Exception('订单已经结算完毕，不可投诉');
				}
				if ($order['get_card_password'] && $order['get_card_password'] != $param['get_card_password']) {
					throw new Exception('查询密码不正确');
				}
				// 开启事务
				Db::startTrans();
				$add = [];
				$add['content'] = $param['content'];
				$add['type'] = $param['type'];
				$add['qq'] = $param['qq'];
				$add['mobile'] = $param['mobile'];
				$add['order_sn'] = $param['order_sn'];
				$add['order_id'] = $order['id'];
				$add['store_id'] = $order['store_id'];
				$add['good_id'] = $order['good_id'];
				$add['user_id'] = $order['good_id'];
				$complaint = ComplaintModel::create($add);
				if (!$complaint) {
					throw new Exception('提交异常');
				}
				$order['lock_status'] = 1;
				$order->save();
				$log = [];
				$log['message'] = '用户提交投诉：' . $param['content'];
				$log['role'] = '用户';
				$log['order_id'] = $order['id'];
				$log['store_id'] = $order['store_id'];
				$log['good_id'] = $order['good_id'];
				$complaint->logs()->save($log);
				// 订单通知及统计
				$param = [];
				$param['order_id'] = $order['id'];
				$param['join_queue'] = true;
				$param['type'] = 'complaint_add_order';
				// 发送商户消息
				NoticeEvent::send($param,$order->store->user);
				// 提交事务
				Db::commit();
				$data['code'] = 0;
				$data['message'] = '提交成功';
			} catch (\Exception $e) {
				// 回滚事务
				Db::rollback();
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}

		/**
		 * 投诉订单
		 * @param array $param
		 * @param UserModel $admin
		 * @return boolean
		 */
		public function check($param, $admin = null)
		{
			try {
				$complaint = ComplaintModel::where('id', $param['id'])->find();
				if (!$complaint) {
					throw new Exception('投诉不存在');
				}
				// 开启事务
				Db::startTrans();
				$complaint['status'] = 1;
				if (!$complaint->save()) {
					throw new Exception('投诉分配失败');
				}
				$log = [];
				$log['message'] = '管理员审核通过，分配商家处理';
				//                $log['role'] = 'admin';
				$log['role'] = '管理员';
				$complaint->logs()->save($log);
				// 发送用户消息
				// 提交事务
				Db::commit();
				$data['code'] = 0;
				$data['message'] = '管理员审核通过';
			} catch (\Exception $e) {
				// 回滚事务
				Db::rollback();
				Log::record('订单取消出错：' . $e->getMessage());
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}

		/**
		 * 投诉订单留言
		 * @param array $param
		 * @param UserModel $user
		 * @param string $role
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 * @datetime: 2020/2/14 16:06
		 */
		public function reply($param, $user = null, $role = '管理员')
		{
			try {
				$complaint = ComplaintModel::where('id', $param['id'])->find();
				if (!$complaint) {
					throw new Exception('投诉不存在');
				}
				if (1 == $complaint['result']) {
					throw new Exception('投诉已经处理结束');
				}
				$log = [];
				$log['role'] = $role;
				$log['user_id'] = $user['id'];
				// 开启事务
				Db::startTrans();
				if ('用户' == $role) {
					$log['message'] = '用户留言：' . $param['message'];
					if (2 == $param['result']) {
						// 平台介入
						$complaint['status'] = 2;
						$log['message'] .= ';并申请平台介入';
					}
				} elseif ('商家' == $role) {
					if (2 == $param['result']) {
						// 商家申请平台介入
						$complaint['status'] = 2;
						$log['message'] = '商家申请平台介入';
					}elseif (1 == $param['result'])
					{
						$complaint['status'] = 5;
						$log['message'] = '商家认同投诉，订单自动取消以及请买家注意相关退款';

						$order = new OrderEvent();
						$order->complaintFinish($complaint);
					}
					else {
						$log['message'] = '商家留言：' . $param['message'];
					}
				} elseif ('管理员' == $role) {
					$log['message'] = '管理员判断投诉'.(1 ==$param['result']?'“成立”':'“失败”').'留言信息：'.$param['message'];
					$complaint['result'] = $param['result'];
					$complaint['status'] = 5;
					$order = new OrderEvent();
					$order->complaintFinish($complaint);
				} else {
					// 在没有平台介入情况下 根据最后一次留言判断
					$last_log = $complaint->logs()->where('create_time DESC')->find();
					if ('用户' == $last_log['role']) {
						$complaint['result'] = 1;
					} else {
						$complaint['result'] = 0;
					}

					$order = new OrderEvent();
					$order->complaintFinish($complaint);
				}
				$complaint->save();
				$complaint->logs()->save($log);
				// 提交事务
				Db::commit();
				$data['code'] = 0;
				$data['message'] = '信息提交成功';
			} catch (\Exception $e) {
				// 回滚事务
				Db::rollback();
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}




	}
